<?php
namespace App\Library\Impl;
use App\Models\DawnOss;
use Illuminate\Support\Facades\Log;
use App\Library\UserLibraryInterface;
use Carbon\Carbon;
use Illuminate\Support\Facades\Hash;
use App\Pojo\ResponsePojo;
use App\Library\ZhjyLibraryInterface;
use Illuminate\Support\Facades\Auth;
use App\Models\DawnZhjyUser;
use App\Library\FileLibraryInterFace;

class FileLibraryImpl implements FileLibraryInterFace{
    public function getImageList($currentPage, $pageSize){
        
        return DawnOss::where('user_id', Auth::id())
            ->where('file_type', '=', 'image')
            ->where('status', '=', 1)
            ->orderBy('created_at', 'desc')
            ->paginate($pageSize, ['*'], 'page', $currentPage);
    }
    public function deleteImageMager($credentials){
        foreach ($credentials['ids'] as $id) {
            DawnOss::where('id', $id)->update(['status' => 0]);
        }
    }


    
    public function deleteFileByHashsStatus($hashs){
        if (empty($hashs)) {
            return;
        }
        DawnOss::whereIn('file_hash', $hashs)
            ->update(['status' => 0]);
    }

    public function rowbackFileByHashsStatus($hashs){
        if (empty($hashs)) {
            return;
        }
        DawnOss::whereIn('file_hash', $hashs)
            ->update(['status' => 1]);
    }

    public function getImageListByHashs($fileHashs){
        $hashs = [];        
        $hashArray = explode(',', $fileHashs[0]);
        
        // 遍历每个hash值
        foreach ($hashArray as $hash) {
            // 如果hash值包含逗号，则继续分割
            if (strpos($hash, ',') !== false) {
            } else {
                $hashs[] = $hash;
            }
        }
        $files = [];
        foreach($hashs as $hash){
            $file = DawnOss::where('file_hash', $hash)
                ->where('status', 1)
                ->first();
            if ($file) {
                $files[] = json_decode($file);
            }
        }
        
        return $files;
    }

}